using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccess
{
    public partial class LogDao : IDao<Log>
    {
        public Log Select(Guid id)
        {
            Log log = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Log_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        log = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return log;
        }

        public void Insert(Log log)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Log_Insert";
            command.Parameters.Add(new SqlParameter("@Id", log.Id));
            command.Parameters.Add(new SqlParameter("@Level", log.Level));
            command.Parameters.Add(new SqlParameter("@Source", log.Source));
            command.Parameters.Add(new SqlParameter("@Category", log.Category));
            command.Parameters.Add(new SqlParameter("@EventId", log.EventId));
            command.Parameters.Add(new SqlParameter("@UserName", log.UserName));
            command.Parameters.Add(new SqlParameter("@Data", log.Data));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(Log log)
        {
            if (log.UpdateTimestamp == DateTime.MinValue) log.UpdateTimestamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Log_Update";
            command.Parameters.Add(new SqlParameter("@Id", log.Id));
            command.Parameters.Add(new SqlParameter("@Level", log.Level));
            command.Parameters.Add(new SqlParameter("@Source", log.Source));
            command.Parameters.Add(new SqlParameter("@Category", log.Category));
            command.Parameters.Add(new SqlParameter("@EventId", log.EventId));
            command.Parameters.Add(new SqlParameter("@UserName", log.UserName));
            command.Parameters.Add(new SqlParameter("@Data", log.Data));
            command.Parameters.Add(new SqlParameter("@UpdateTimestamp", DateTime.Now));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Log_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Private Method

        internal static Log CreateEntityByReader(IDataReader reader)
        {
            Log log = new Log();
            if (reader["Id"] != DBNull.Value) log.Id = (Guid)reader["Id"];
            if (reader["Level"] != DBNull.Value) log.Level = reader["Level"] as string;
            if (reader["Source"] != DBNull.Value) log.Source = reader["Source"] as string;
            if (reader["Category"] != DBNull.Value) log.Category = reader["Category"] as string;
            if (reader["EventId"] != DBNull.Value) log.EventId = reader["EventId"] as string;
            if (reader["UserName"] != DBNull.Value) log.UserName = reader["UserName"] as string;
            if (reader["Data"] != DBNull.Value) log.Data = reader["Data"] as string;
            if (reader["InsertTimestamp"] != DBNull.Value) log.InsertTimestamp = (DateTime)reader["InsertTimestamp"];
            if (reader["UpdateTimestamp"] != DBNull.Value) log.UpdateTimestamp = (DateTime)reader["UpdateTimestamp"];

            return log;
        }

        #endregion
    }
}